<template>
  <dialog-editor ref="dialog"></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'
import {mRequest} from '@/utils/request'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      dataFormConfig: {
        id: {
          hidden: true
        },
        location: {
          label: '模具所在地'
        },
        productId: {
          label: '产品编号',
          type: 'list',
          clearable: true,
          placeholder: '请选择模产品编号',
          list: [],
          selectCallback: this.productSelectCallback
        },
        modelNo: {
          label: '模具编号',
          placeholder: '请输入模具编号'
        },
        modelCategoryId: {
          label: '模具类别',
          type: 'list',
          placeholder: '请选择模具类别',
          list: []
        },
        //
        modelDrawingId: {
          label: '模具图纸',
          type: 'uploadImage',
          imageType: 'uploadProductPaper'
        },
        remark: {
          inputType: 'textarea',
          label: '备注',
          placeholder: '请输入备注'
        },
        status: {
          label: '状态',
          type: 'switch',
          activeValue: 1,
          inactiveValue: 0,
          default: 1
        }
      }
    }
  },
  methods: {
    init(id, property, cb) {
      this.dataForm.id = id || 0
      this.initModelCategoryList()
   //   this.initClientInfoList('clientId')
      this.initProductInfoList()

      this.$refs.dialog.initDialog({
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        dataRule: this.getNeedDataRule(  'modelNo','status'),
        initApi: 'modelApi/modelInfoInfo',
        saveApi: 'modelApi/saveModelInfo',
        updateApi: 'modelApi/updateModelInfo',
        objectName: 'modelInfo',
        submitCallback: cb
      })

      this.$nextTick(() => {

        if (!this.R.isNil(property)) {
          Object.keys(property).forEach((key) => {
            this.dataForm[key] = property[key]
          })
        }
      })
    },
    addNewProduct() {
      const cb = (newValue) => {
        this.initProductInfoList()
        this.dataForm.productId=newValue.id
      }
      this.$parent.openProductInfoDialog(cb)
    },
    productSelectCallback(value){
      if (this.R.isEmpty(value)||this.R.isNil(value)) {
        return
      }
      mRequest('productApi/productInfoInfo', {id: value}, (res) => {
        const productInfo = this.R.path(['data', 'productInfo'], res)

        this.dataForm['modelNo'] = productInfo['productNo']

      })
    }
  }
}
</script>
